package com.xiaoyi;

import com.xiaoyi.httplibrary.OKHttpUtils;
import com.xiaoyi.httplibrary.ProgressListener;
import com.xiaoyi.threadpoollibrary.ThreadPool;
import com.xiaoyi.threadpoollibrary.ThreadPoolManager;
import com.xiaoyi.utils.JdbcUtils;
import okhttp3.*;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;


public class MainTest {
    private static Logger _logger = LoggerFactory.getLogger(MainTest.class);
    public static void main(String[] args) {
//        uploadFile();
//        doGet();
//        getDataFromDb();
//        download();
        ThreadPoolManager tpm = ThreadPoolManager.getSingleton();
        tpm.init(); // 在应用启动时调用

        ThreadPool threadPool = tpm.getThreadPool();
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                download(1);
            }
        });
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                download(2);
            }
        });
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                download(3);
            }
        });
        tpm.destroy(); // 在应用关闭时调用

    }




    public static void getDataFromDb(){
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            Object query = queryRunner.query("select count(1) from baidu;", new ScalarHandler<Object>() {
            });
            _logger.info(query.toString());
            System.out.println(query.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    public  static void doGet()  {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "com/com.xiaoyi");
        String res = OKHttpUtils.doGet("https://www.ipip.net/ip/",map );
        System.out.println("------res---------->"+res);
    }

    public  static void doAsyGet()  {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "com/com.xiaoyi");
        OKHttpUtils.doAsyGet("http://127.0.0.1:80/test/1.php",map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("------res---------->"+e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("------res---------->"+response.body().string());
            }
        });
    }


    public  static void doPost(){
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("name", "com/com.xiaoyi");
        OKHttpUtils.doAsyPost("http://127.0.0.1:80/1/1.php", stringStringHashMap, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());
            }
        });
    }

    //下载文件
    public  static  void download(int type){
        UUID uuid = UUID.randomUUID();
        OKHttpUtils.downloadAndSaveFile("http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg", "D:/temp/"+uuid.toString()+".avi", new ProgressListener() {
            @Override
            public void onProgress(long currentBytes, long contentLength, boolean done) {
                int progress = (int) (currentBytes * 100 / contentLength);
                System.out.println("---------"+Thread.currentThread()+"--"+type+"--->"+progress);
            }
        });
    }

    public static void uploadFile() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("C:\\Users\\Administrator\\Desktop\\MD5.txt");
        strings.add("C:\\Users\\Administrator\\Desktop\\OkHttp3_MultiFile-master (1).zip");
        OKHttpUtils.uploadFile("http://127.0.0.1:80/upload/upload.php", strings, new ProgressListener() {
            @Override
            public void onProgress(long currentBytes, long contentLength, boolean done) {
//                int progress = (int) (currentBytes * 100 / contentLength);
//                System.out.println("---------onProgress----->"+progress);
                System.out.println("-------contentLength--------->"+contentLength);
                System.out.println("-------currentBytes--------->"+currentBytes);

            }
        }, new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {

                System.out.println("------onFailure-------->"+ e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("--------onResponse------>"+response.body().string());
            }
        });
    }
}
